﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GlobalGameClasses;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Sirian;

namespace InterfaceComponents
{
    /// <summary>
    /// This represents a Buttoncomponent. It can be activated by Mouseclick or by selecting it with the arrow Keys and pressing enter. 
    /// </summary>
    public class Button : InterfaceComponent, MenuComponent
    {
	    public event GlobalGameClasses.Eventhandler.ButtonEventHandler buttonClick;

	    public string name
	    {
		    get;
		    private set;
	    }

        //This is used for the interaction with the mouse.
	    private Rectangle border
	    {
		    get;
		    set;
	    }

        public SirianGame.Modes next
        {
            get;
            private set;
        }

        public SirianGame.Modes parent
        {
            get;
            private set;
        }

	    private SpriteFont spriteFont;

	    public Button(Rectangle border, string name, Vector2 position, SpriteBatch spriteBatch, SpriteFont spriteFont, SirianGame.Modes next, SirianGame.Modes parent)
	    {
            this.border = border;
            this.name = name;
            this.position = position;
            this.spriteBatch = spriteBatch;
            this.spriteFont = spriteFont;
            this.next = next;
            this.parent = parent;
	    }

        /// <summary>
        /// Draws the Button with the specified color.
        /// </summary>
        /// <param name="color">Color of the font.</param>
        public override void Draw(Color color)
	    {
            spriteBatch.DrawString(spriteFont, name, position - new Vector2(2,0), Color.Black);
            spriteBatch.DrawString(spriteFont, name, position + new Vector2(2, 0), Color.Black);
            spriteBatch.DrawString(spriteFont, name, position - new Vector2(0, 2), Color.Black);
            spriteBatch.DrawString(spriteFont, name, position + new Vector2(0, 2), Color.Black);
            spriteBatch.DrawString(spriteFont, name, position, color);
	    }

        /// <summary>
        /// Checks if the button is pressed.
        /// </summary>
        /// <param name="selected">Whether the user selected the button with the keyboard</param>
	    public virtual void Update(KeyboardState keyboard, KeyboardState oldKeyboard, MouseState mouse, MouseState oldMouse, bool selected)
	    {
		    if (selected && KeyPressed(keyboard, oldKeyboard, Keys.Enter))
            {
                buttonClick(this);
            }
            else if (oldMouse.LeftButton == ButtonState.Pressed && mouse.LeftButton == ButtonState.Released && border.Intersects(new Rectangle(mouse.X,mouse.Y,1,1)))
            {
                buttonClick(this);
            }
	    }

	    private bool KeyPressed(KeyboardState keyboard, KeyboardState oldKeyboard, Keys key)
	    {
            return oldKeyboard.IsKeyDown(key) && keyboard.IsKeyUp(key);
	    }
    }
}

